/*
 * Copyright (C) 2004, Velocityme Partnership
 * ButtonAction.java
 *
 * Created on 01 September 2004, 09:05
 */

package com.velocityme.www.interfaces;

import com.velocityme.entity.PermissionBean;
//web imports
import com.velocityme.www.dto.*;
import com.velocityme.www.actionforms.*;
import com.velocityme.www.utility.NodeUtil;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.*;

import org.apache.commons.beanutils.*;
//ejb interfaces
import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.nodeactionforms.IssueValueActionForm;
import com.velocityme.www.actionforms.nodeactionforms.UserValueActionForm;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;

/**
 *
 * @author  Robert
 */
public class ButtonAction extends org.apache.struts.action.Action {
    
    /** Creates a new instance of ButtonAction */
    public ButtonAction() {
    }
    
    public ActionForward execute(ActionMapping mapping, 
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
    
        try {
            ButtonActionForm buttonActionForm = (ButtonActionForm)form;
            HttpSession session = request.getSession();
            KeySession key = (KeySession)session.getAttribute("sessionKey");
            RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
            NodePK nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
            NodeValue nodeValue = cs.getNodeValue(key, nodePK);
            if (buttonActionForm.getButton().equals("EditNode")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_NODE_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditSprint")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_SPRINT_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditSystemProperty")) {
                return mapping.findForward("ACTION_SYSTEMPROPERTY_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditContactDetailType")) {
                return mapping.findForward("ACTION_CONTACTDETAILTYPE_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("AddLicence")) {
                return mapping.findForward("ACTION_ADDLICENCE_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditProject")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_PROJECT_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditAnchor")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_ANCHOR_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditAction")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_ACTION_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditIssue")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_ISSUE_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditOrganisation")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_ORGANISATION_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditDepartment")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_DEPARTMENT_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditPerson")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_PERSON_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditUser")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_USER_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditGroup")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_GROUP_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditState")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_STATE_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditStatus")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_STATUS_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditTaskStateMachine")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_TASKSTATEMACHINE_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("StateMachine")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_STATEMACHINE_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditRole")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_ROLE_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("EditNote")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_NOTE_EDIT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("Comment")) {
                return mapping.findForward("ACTION_POST_COMMENT_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("Password")) {
                session.setAttribute("enterOldPassword", new Boolean(!cs.getAssignedPermissions(key, nodePK).contains(new PermissionPK(new Integer(PermissionBean.USER_CHANGE_PASSWORD)))));
                return mapping.findForward("ACTION_CHANGE_PASSWORD_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("ChangeState")) {
                TaskPK taskPK = new TaskPK(nodePK.getNodeId());
                TaskValue taskValue = cs.getTaskValue(key, taskPK);
                Collection integerStrings = new ArrayList();
                Iterator i = cs.getTaskLegalTransitionValues(key, taskPK).iterator();
                while(i.hasNext()) {
                    TransitionValue transitionValue = (TransitionValue)i.next();
                    integerStrings.add(new IntegerString(transitionValue.getTransitionId(), transitionValue.toString()));
                }
                session.setAttribute("transitionPairs", integerStrings);
                Collection completeIntegerStrings = new ArrayList();
                for (int j = 0; j <= 100; j+=5)
                    completeIntegerStrings.add(new IntegerString(new Integer(j), Integer.toString(j)));
                session.setAttribute("completePairs", completeIntegerStrings);
                session.setAttribute("permissionTimeCreate", new Boolean(cs.getAssignedPermissions(key, new NodePK(taskPK.getTaskId())).contains(new PermissionPK(new Integer(PermissionBean.TIME_CREATE)))));
                session.setAttribute("years", NodeUtil.getInstance().getYears());
                session.setAttribute("months", NodeUtil.getInstance().getMonths());
                session.setAttribute("days", NodeUtil.getInstance().getDays());
                session.setAttribute("hours", NodeUtil.getInstance().getHours());
                session.setAttribute("durationDays", NodeUtil.getInstance().getDurationDays());
                session.setAttribute("durationHours", NodeUtil.getInstance().getDurationHours());
                session.setAttribute("minutes", NodeUtil.getInstance().getMinutes());
                // setup default values
                Calendar now = Calendar.getInstance();
                ChangeStateActionForm changeStateActionForm = new ChangeStateActionForm();
                changeStateActionForm.setPercentageComplete(taskValue.getPercentageComplete().byteValue());
                changeStateActionForm.setDurationTotalMins(taskValue.getRemainingDuration_min());
                changeStateActionForm.setAssignTime(taskValue.getStateValue().getIsActive().booleanValue());
                changeStateActionForm.setYear(now.get(Calendar.YEAR));
                changeStateActionForm.setMonth(now.get(Calendar.MONTH));
                changeStateActionForm.setDay(now.get(Calendar.DATE));
                Calendar start = Calendar.getInstance();
                start.setTime(cs.getTaskLastStateChangeTime(key, taskPK));
                changeStateActionForm.setStateId(taskValue.getStateValue().getStateId().intValue());
                changeStateActionForm.setStartHour(start.get(Calendar.HOUR_OF_DAY));
                changeStateActionForm.setStartMinute(start.get(Calendar.MINUTE)/5*5);
                changeStateActionForm.setStopHour(now.get(Calendar.HOUR_OF_DAY));
                changeStateActionForm.setStopMinute(now.get(Calendar.MINUTE)/5*5);
                session.setAttribute("changeStateActionForm", changeStateActionForm);
                return mapping.findForward("ACTION_CHANGE_STATE_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("ReassignResponsibility")) {
                TaskPK taskPK = new TaskPK(nodePK.getNodeId());
                TaskValue taskValue = cs.getTaskValue(key, taskPK);
                session.setAttribute("allContactablePairs", NodeUtil.getAllContactablePairs(key, cs));
                Collection responsibleContactableIntegerStrings = new ArrayList();
                Iterator i = cs.getTaskResponsibleContactableValues(key, taskPK).iterator();
                while(i.hasNext()) {
                    ContactableValue contactableValue = (ContactableValue)i.next();
                    responsibleContactableIntegerStrings.add(new IntegerString(contactableValue.getContactableId(), ContactableValueToString.toString(contactableValue)));
                }
                session.setAttribute("responsibleContactablePairs", responsibleContactableIntegerStrings);
                session.setAttribute("reassignResponsibilityActionForm", new ReassignResponsibilityActionForm());
                return mapping.findForward("ACTION_REASSIGN_RESPONSIBILITY_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("CreateTime")) {
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_CREATE_TIME_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("ToggleUserEnabled")) {
                UserPK userPK = new UserPK(nodePK.getNodeId());
                UserValue userValue = cs.getUserValue(key, userPK);
                session.setAttribute("userValue", userValue);
                return mapping.findForward("ACTION_TOGGLE_USER_ENABLED_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("ToggleUserInterest")) {
                return mapping.findForward("ACTION_TOGGLE_USER_INTERESTED_SUCCESS");
            }
            if (buttonActionForm.getButton().equals("UpdateTime")) {
                TaskPK taskPK = new TaskPK(nodePK.getNodeId());
                cs.taskRecursiveUpdateTimes(key, taskPK);
                request.setAttribute("nodePK", nodePK);
                return mapping.findForward("ACTION_UPDATE_TIME_SUCCESS");
            }
        }
        catch(Exception e){
            request.setAttribute("Additional_Info", e);
        }
        return mapping.findForward("ACTION_BUTTON_FAILED");
    }
    
}
